package org.payeco.risk.service.impl;

import java.util.List;
import java.util.Set;

import org.payeco.risk.api.PayeocRiskRuleEngine;
import org.payeco.risk.common.PayecoRiskConstant;
import org.payeco.risk.entity.PayecoRiskRule;
import org.payeco.risk.entity.PayecoRuleModular;
import org.payeco.risk.model.FunctionResult;
import org.payeco.risk.model.UserDefinedClasses;
import org.payeco.risk.model.UserDefinedParams;
import org.payeco.risk.model.UserDefinedReturnClass;
import org.payeco.risk.service.PayecoRiskValidatorService;
import org.payeco.risk.service.PayecoRuleModularService;
import org.payeco.risk.service.PayecoRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * @author yps
 * payeco 风控服务提供者
 */
@Service
public class PayecoRiskValidatorServiceImpl implements PayecoRiskValidatorService<FunctionResult> {
	
	@Autowired
	private PayecoRuleService ruleService;
	@Autowired
	private PayecoRuleModularService<String> modularService;
	@Autowired
	private PayeocRiskRuleEngine<FunctionResult> ruleEngine;
	
	
	/* (non-Javadoc)
	 * @see 事前验证，风控校验入口
	 */
	@Override
	public UserDefinedReturnClass<FunctionResult> forwardValidate(
			UserDefinedClasses classList, UserDefinedParams params) {
		String sysmerchantNo = params.getStr(PayecoRiskConstant.SYSMERCHANT.getValue());	
		String bank = params.getStr(PayecoRiskConstant.BANK.getValue());
		String industry = params.getStr(PayecoRiskConstant.INDUSTRY.getValue());
		String accountType = params.getStr(PayecoRiskConstant.ACCOUNTTYPEDF.getValue());
		String userLevel = params.getStr(PayecoRiskConstant.USERLEVEL.getValue());
		
		List<PayecoRuleModular> forwardModulars = modularService.findModularWithParamTypes(bank, sysmerchantNo, industry, accountType, userLevel, "CheckMode.Forward");
		List<PayecoRuleModular> doMainModulars = modularService.findModularWithParamTypes(bank, sysmerchantNo, industry, accountType, userLevel, "CheckMode.DoMain");
		List<PayecoRuleModular> backwardModulars = modularService.findModularWithParamTypes(bank, sysmerchantNo, industry, accountType, userLevel, "CheckMode.Backward");
		
		PayecoRiskRule rule = new PayecoRiskRule();
		rule.setCheckmode("CheckMode.Forward");
		rule.setState("State.Enabled");
		List<PayecoRiskRule> rules = Lists.newArrayList();
		
		for(PayecoRuleModular modular : forwardModulars){
			String modularNo = modular.getModularNo();
			List forwardrules = ruleService.findRiskruleListWithGroup(rule, modularNo);
			rules.addAll(forwardrules);
		}
		
		Set<String> merchantGroups = Sets.newHashSet();
		for(PayecoRuleModular modular : doMainModulars){
			String modularNo = modular.getModularNo();
			merchantGroups.add(modularNo);
		}
		List domainrules = ruleService.findRiskruleListWithGroups(rule, merchantGroups);
		rules.addAll(domainrules);
		
		for(PayecoRuleModular modular : backwardModulars){
			String modularNo = modular.getModularNo();
			List backwardrules = ruleService.findRiskruleListWithGroup(rule, modularNo);
			rules.addAll(backwardrules);
		}
		return ruleEngine.executeOldRules(rules, classList, params);
	}

	/* (non-Javadoc)
	 * @see 事后更新，风控相关表更新
	 */
	@Override
	public UserDefinedReturnClass<FunctionResult> backwardValidate(
			UserDefinedClasses classList, UserDefinedParams params) {
		// TODO Auto-generated method stub
		return null;
	}

}
